# -*- coding: utf-8 -*-
# 导入库pip install openpyxl -i https://pypi.tuna.tsinghua.edu.cn/simple
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.metrics import mean_squared_error  # 评价指标
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM, GRU
from keras import optimizers
import keras
import tensorflow as tf
#  mse rmse mae rmape
#  adam sgd
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
import warnings
warnings.filterwarnings("ignore")  # 忽略一些警告 不影响运行
from date_process import data_read_csv
train_x,train_y=data_read_csv()
train_labels_one_hot = tf.keras.utils.to_categorical(train_y, num_classes=2)
print(train_labels_one_hot.shape)
train_y=train_labels_one_hot
# 序列长度
int_sequence_len = train_x.shape[1]
# 每个序列的长度
int_a = 1

# 输出几个元素 几步：
out_len = train_y.shape[1]

# 划分验证集和测试集
x_train, x_test, y_train, y_test = train_test_split(np.array(train_x), np.array(train_y), test_size=0.2, random_state=1)

print(x_train.shape)
print(len(x_train), len(x_test))  # 1243 311
x_train = x_train.reshape(len(x_train),-1) # 三维度数据 全部数据长度 序列长度 每个序列维度
y_train = y_train.reshape(len(x_train),out_len)

print(x_train.shape)
print(y_train.shape)
x_test = x_test.reshape(len(x_test),-1)
y_test = y_test.reshape(len(x_test),out_len)

print(x_test.shape)
print(y_test.shape)


def create_model_1():
    model = keras.models.Sequential([
        keras.layers.Dense(1024, activation='relu',input_shape=(int_a*int_sequence_len,)),  # 全连接
        keras.layers.Dense(512, activation='relu'),  # 全连接
        keras.layers.Dense(64, activation='relu'),  # 全连接
        keras.layers.Dense(2,activation='softmax'), # 1个全链接
    ])
    model.compile(loss=['binary_crossentropy'], metrics=["categorical_accuracy"], optimizer='Adam')  # 回归损失函数和优化器 Adam SGD
    return model

model1 = create_model_1()
model1.summary()
history=model1.fit(x_train, y_train, validation_data=(x_train, y_train), epochs=50, batch_size=16, shuffle=True)
#                                                                训练世代      batch
model1.save_weights('lstmmoxing')  # 模型保存

import matplotlib.pyplot as plt
training_loss = history.history['loss']
test_loss = history.history['val_loss']
# 创建迭代数量
epoch_count = range(1, len(training_loss) + 1)
# 可视化损失历史
plt.plot(epoch_count, training_loss, 'r--')
plt.plot(epoch_count, test_loss, 'b-')
plt.legend(['Training Loss', 'Test Loss'])
plt.title("train loss and test loss")
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()


from sklearn.metrics import mean_squared_error  # 均方误差
from sklearn.metrics import mean_absolute_error  # 平方绝对误差
from sklearn.metrics import r2_score  # R square

# 调用
# 引用上边的模型实例
model_jiazai_1 = create_model_1()
# 加载保存好的模型
model_jiazai_1.load_weights('lstmmoxing')

y1_pred_lstm = model_jiazai_1.predict(x_test)

y_true=[]
y_pred=[]
print(y1_pred_lstm.shape)
print(y_test.shape)
for i in range(len(y1_pred_lstm)):
    # print(np.argmax(y1_pred_lstm[i]))
    # print(np.argmax(y_test[i]))
    y_true.append(np.argmax(y_test[i]))
    y_pred.append(np.argmax(y1_pred_lstm[i]))
from metra import acc_metra
acc_metra(y_true, y_pred,label=['0','1'])


# 输出到excle
name = ['真实值', '预测值']
test = pd.DataFrame(columns=name, data=result)
test.to_excel('result_天气.xlsx')

